var TemplateConfig = function (type, config) {
  BaseModel.call(this);

  var addFields = [];
  switch (type) {
    case 'purchaseOrder':
      addFields = ['hasSumBtn', 'showOrderTableSum', 'showWarehouseTableSum', 'hasImgBtn', 'showTableImg',
        'showEmail', 'addRowRemark', 'addDelRow', 'orderGroup', 'warehouseGroup', 'orderMerge', 'warehouseMerge'
      ];
      break;
    case 'prePurchaseOrder':
      addFields = ['showEmail', 'addRowRemark', 'addDelRow'];
      break;
  }

  var sizeList = {
    "A3": {
      unit: "mm",
      width: 297,
      height: 420
    },
    "A4": {
      unit: "mm",
      width: 210,
      height: 297
    },
    "A5": {
      unit: "mm",
      width: 148,
      height: 210
    },
    "B3": {
      unit: "mm",
      width: 353,
      height: 500
    },
    "B4": {
      unit: "mm",
      width: 250,
      height: 353
    },
    "B5": {
      unit: "mm",
      width: 176,
      height: 250
    }
  };
  this.columnNames = [];

  var self = this;
  var fn = {
    initData: function (type, config) {
      self.templateType = type;
      this.initEmptyObject(type);
      if (config !== undefined) {
        this.resetObject(type, config);
      }
    },

    initEmptyObject: function (type) {
      var sizeType, direction,
        width = undefined,
        height = undefined,
        unit = '';
      var clauseContent;
      var textSize = {
        titleSize: 20,
        tableSize: 14,
        otherSize: 14
      };
      switch (type) {
        case 'purchaseOrder':
          sizeType = 'A4';
          direction = 'vertical';
          clauseContent =
            '<div>1.严格按照订单数量交货，保证品质，不准短缺，多不付款。</div><div>2.严格遵守交货日期，逾期我司有权拒绝收货。</div><div>3.经检验不合格产品，将予以退回。</div><div>4.在我司生产过程中，如因板料脱皮，起层等现象我司将追究赔偿责任。</div>';
          self.hasSumBtn = true;
          self.showOrderTableSum = true;
          self.showWarehouseTableSum = false;
          self.hasImgBtn = true;
          self.showTableImg = false;
          self.showEmail = false;
          self.addRowRemark = true;
          self.addDelRow = true;
          self.orderGroup = ['orderId', 'materialCode'];
          self.warehouseGroup = ['orderId', 'materialCode'];
          self.orderMerge = ['orderId', 'materialCode', 'price', 'unit'];
          self.warehouseMerge = ['orderId', 'materialCode', 'price', 'unit'];
          break;
        case 'prePurchaseOrder':
          sizeType = 'A4';
          direction = 'vertical';
          clauseContent =
            '<div>1.严格按照订单数量交货，保证品质，不准短缺，多不付款。</div><div>2.严格遵守交货日期，逾期我司有权拒绝收货。</div><div>3.经检验不合格产品，将予以退回。</div><div>4.在我司生产过程中，如因板料脱皮，起层等现象我司将追究赔偿责任。</div>';
          self.showEmail = false;
          self.addRowRemark = true;
          self.addDelRow = true;
          break;
        case 'warehouseInoutOrder':
        case 'warehouseInOrder':
          sizeType = 'A4';
          direction = 'horizontal';
          clauseContent = "";
          textSize.otherSize = 16;
          break;
        case 'outOrder':
          sizeType = 'A4';
          direction = 'horizontal';
          clauseContent = "";
          break;
        case 'issueOrder':
          sizeType = 'A4';
          direction = 'horizontal';
          clauseContent = "";
          break;
        case 'returnOrder':
          sizeType = 'default';
          unit = 'cm';
          width = 14;
          height = 23;
          direction = 'horizontal';
          clauseContent = "";
          break;
      }

      self.size = {
        type: sizeType,
        direction: direction,
        unit: unit,
        width: width,
        height: height
      };
      self.textSize = textSize;
      self.clauseContent = clauseContent;
      self.resetPageSize();
    },

    resetObject: function (type, config) {
      var fields = ['size', 'textSize', 'clauseContent'];
      fields.pushArray(addFields);

      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var value = config[field];
        if (value !== undefined) {
          self[field] = value;
        }
      }

      var columnConfig = config.column;
      if (columnConfig !== undefined) {
        for (var field in columnConfig) {
          var columns = columnConfig[field];
          self.initColumns(columns, field);
        }
      }
    }
  };

  this.resetPageSize = function () {
    var size = this.size;
    var type = size.type;
    var sizeObj = sizeList[type];
    if (sizeObj !== undefined) {
      size.unit = sizeObj.unit;
      size.width = sizeObj.width;
      size.height = sizeObj.height;
    }
  };

  this.getSizePrecision = function () {
    var unit = this.size.unit;
    if (unit === 'cm') {
      return 2;
    }
    return 0;
  };

  this.initColumns = function (columns, field) {
    if (field === undefined) {
      field = 'columns';
    }
    var data = this[field];
    if (data !== undefined) {
      return;
    }

    var templateColumns = TemplateColumnConfigUtil.getTemplateColumn(field);
    for (var i = 0; i < columns.length; i++) {
      var column = columns[i];
      if (column.id === undefined) {
        column.id = SysUtil.getId();
      }

      if (column.hasSort === undefined) {
        column.hasSort = false;
      }

      var templateColumn = _.find(templateColumns, {
        field: column.field
      });
      if (templateColumn !== undefined && !templateColumn.noUsedSort) {
        column.canSort = true;
      } else {
        column.canSort = false;
      }
    }

    if (this.columnNames.indexOf(field) === -1) {
      this.columnNames.push(field);
    }
    this[field] = columns;
  };

  this.checkValid = function () {
    var checkColumnNames = this.columnNames;
    var columnFields = ['label', 'width', 'field'];
    for (var i = 0; i < checkColumnNames.length; i++) {
      var checkColumnName = checkColumnNames[i];
      var checkColumns = this[checkColumnName];
      if (checkColumns === undefined) {
        continue;
      }

      for (var j = 0; j < checkColumns.length; j++) {
        var checkColumn = checkColumns[j];
        var code = CheckVaildUtil.checkNullField(columnFields, checkColumn);
        if (code !== '') {
          return [checkColumnName, j, code];
        }
      }
    }
    return '';
  }

  this.getObject = function () {
    var postData = {
      size: this.size,
      textSize: this.textSize,
      clauseContent: this.clauseContent
    };

    this.setPostData(postData, addFields, false);

    var columnNames = this.columnNames;
    var tableColumn = {};
    for (var i = 0; i < columnNames.length; i++) {
      var columnName = columnNames[i];
      var columns = this[columnName];
      var columnList = [];
      for (var j = 0; j < columns.length; j++) {
        var column = columns[j];
        var obj = {
          label: column.label,
          field: column.field,
          width: column.width
        };
        var hasSort = column.hasSort;
        if (hasSort) {
          obj.hasSort = hasSort;
        }
        columnList.push(obj);
      }
      tableColumn[columnName] = columnList;
    }
    postData.column = tableColumn;

    return postData;
  };

  fn.initData(type, config);
};
